import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:递归练习  数组学习
 * User:岁亮亮
 * Date:2023-12-31
 * TIME:10:35
 */

public class Demo {
    public static void main(String[] args) {//出现一次的数  数组除了一个只出现一次别的都出现两次
        int[] array = {2, 2, 3, 55, 55, 6, 7, 8, 6, 7, 8};
        int ret = check4(array);
        System.out.println(ret);
       /* int n=22;
        int a=binary_search2(array,n);
        System.out.println(a);*/
    }

    public static void check3(int[] array) {
        int[] str = new int[array.length];//定义存放重复的数的顺序数组
        int a = 0;
        int b = 0;//顺序数组的长度减一
        int flag = 0;
        String[] str2 = new String[array.length];//定义存放重复了多少次的字符串数组
        int c = 0;//字符串坐标
        int[] str3 = new int[array.length];//存放次数及其排序的数组
        int f = 0;
        for (int i = 0; i <= array.length - 1; i++) {
            String name = "[";
            int count = 0;
            //判断是否是已经确认过次数的内容,没确认过的继续,确认过的跳过
            if ((a = sequential_search2(str, array[i])) == 1) {
                continue;
            }
            if ((a = sequential_search2(str, array[i])) == 0) {
                str[b] = array[i];
                b++;
            }
            flag++;
            for (int j = 0; j <= array.length - 1; j++) {
                if (array[i] == array[j]) {
                    count++;
                }
            }
            fun5(str3, count);
            int e = sequential_search3(str3, count);
            f = (f > e) ? (e) : (f);
            f = e;
            name += array[i] + "出现的次数是" + count + "次]";
            str2[e] = name;
        }
        System.out.println(Arrays.toString(str2));
        System.out.println(Arrays.toString(str3));
        System.out.println("重复次数最少的是 " + str2[f]);
        System.out.println("查找了 " + flag + " 次");
    }

    public static void fun5(int[] array, int n) {//对次数进行排序
        int a = 0;
        array[a] = n;
        a++;
        Arrays.sort(array);
    }

    public static int sequential_search3(int[] array, int n) {//顺序查找
        boolean flag = false;
        for (int i = 0; i <= array.length - 1; i++) {
            if (array[i] == n) {
                flag = true;
                return i;
            }
        }
        if (!flag) {
            return 0;
        }
        return 0;
    }

    public static void check(int[] array) {//查找出现1次的次数其他的是偶数次   1
        int[] str = new int[array.length];
        int a = 0;
        int b = 0;//顺序数组的长度减一
        int flag = 0;

        for (int i = 0; i <= array.length - 1; i++) {
            String name = "[";
            int count = 0;
            if ((a = sequential_search2(str, array[i])) == 1) {
                continue;
            }
            if ((a = sequential_search2(str, array[i])) == 0) {
                str[b] = array[i];
                b++;
            }
            flag++;
            for (int j = 0; j <= array.length - 1; j++) {
                if (array[i] == array[j]) {
                    count++;
                }
            }
            if (count == 1) {
                System.out.println(array[i]);
            }
        }
        System.out.println(flag);
    }

    public static int check4(int[] array) {//查找出现1次的次数其他的是偶数次   1
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }

    public static int sequential_search2(int[] array, int n) {//顺序查找
        boolean flag = false;
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i] == n) {
                flag = true;
                return 1;
            }
        }
        if (!flag) {
            return 0;
        }
        return 0;
    }

    public static void main45(String[] args) {//把奇数放在偶数前面
        int[] array = {1, 1, 4, 4, 4, 5, 5, 6, 7, 8};

    }

    public static boolean fun71(int[] array) {//找出连续出现3次的奇数
        for (int i = 0; i < array[array.length - 3]; i++) {
            if ((array[i] & 1) != 0 && (array[i + 1] & 1) != 0 && (array[i + 2] & 1) != 0) {
                return true;
            }
        }
        return false;
    }

    public static boolean fun7(int[] array) {//连续3个奇数
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 != 0) {
                count++;
                if (count == 3) {
                    return true;
                }
            } else {
                count = 0;
            }
        }
        return false;
    }

    public static void fun(int[] array) {//奇数在前偶数在后
        int[] array3 = new int[array.length];
        int b = 0;
        int a = array.length - 1;
        for (int i = 0; i < array.length; i++) {

            if (array[i] % 2 != 0) {
                array3[b] = array[i];
                b++;
            } else {
                array3[a] = array[i];
                a--;
            }
        }
        System.out.println(Arrays.toString(array3));
    }

    public static void fun22(int[] array) {//奇数在前偶数在后
        int i = 0;
        int j = array.length-1;
        while(i<j){
            while(i<j&&array[i]%2!=0){
                i++;
            }
            //i走完后是偶数准备交换
            while (i<j&&array[j]%2==0){
                j--;
            }
            //j走完后是奇数和i进行交换
            int tmp=array[i];
            array[j]=array[i];
            array[j]=tmp;
        }
    }

    public static void main22(String[] args) {//二分查找
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查找的数");
        int n = scanner.nextInt();
        int[] array = {1, 2, 32, 34, 45, 56, 67, 78, 89};
        int ret = binary_search(array, n);
        System.out.println(ret);
        sequential_search(array, n);
    }

    public static void sequential_search(int[] array, int n) {//顺序查找
        boolean flag = false;
        for (int i = 0; i <= array.length - 1; i++) {
            if (array[i] == n) {
                flag = true;
                System.out.println("找到了下表是 " + i);
            }
        }
        if (!flag) {
            System.out.println("没有找到要查的数");
        }
    }

    public static int binary_search(int[] array, int n) {//二分查找
        int i = 0;
        int j = array.length - 1;
        while (i <= j) {
            int mub = (i + j) / 2;
            if (array[mub] == n) {
                return mub;
            } else if (array[mub] < n) {
                i = mub + 1;
            } else if (array[mub] > n) {
                j = mub - 1;
            }
        }
        return -1;//没有找到
    }

    public static void main10(String[] args) {//求整型数组的平均值;
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        double n = sum_all(array);
        System.out.println(n);
    }

    public static double sum_all(int[] array) {
        int sum = 0;
        for (int x : array) {
            sum += x;
        }
        return (double) sum / ((double) array.length);
    }

    public static void main9(String[] args) {//字符串逆序
        int[] array = {1, 2, 3, 4, 6};
        reserve(array);
        System.out.println(Arrays.toString(array));
    }

    public static void reserve(int[] array) {
        int i = 0;
        int j = array.length - 1;
        while (i < j) {
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
            i++;
            j--;
        }
    }

    public static void main8(String[] args) {//冒泡排序的实现
        int[] array = {1, 2, 3, 22, 4, 66, 5, 44, 32};
        bubble_sort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void bubble_sort(int[] array) {
        if (array == null) {
            return;
        }
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flag = true;
                }
            }
            if (flag == false) {
                return;
            }
        }
    }

    public static void main6(String[] args) {//数组遍历s四种放法
        //1
        int[] array = {1, 2, 3, 4, 5};
        for (int i = 0; i < 5; i++) {
            System.out.print(array[i]);
        }
        System.out.println();
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
        }
        System.out.println();
        //for-each
        for (int x : array) {
            System.out.print(x);
        }
        System.out.println();
        System.out.print(Arrays.toString(array));
    }

    public static void main7(String[] args) {
        int[] array = {1, 2, 33, 4, 66, 55, 44, 8};
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
        //System.out.println(my_toString(array));
    }

    public static String my_toString(int[] array) {//toString的实现
        if (array == null) {
            return null;
        }
        if (array.length == 0) {
            return "[0]";
        }
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str = str + array[i];
            if (i != array.length - 1) {
                str += ", ";
            }
        }
        str += "]";
        return str;
    }

    public static void main3(String[] args) {//数组默认值
       /* int[]array={0};
        System.out.println(array);//打印[I@1b6d3586
        int[]array1=null;
        System.out.println(array1);//null
        char[] array2={0};
        System.out.println(array2);//
        boolean[] array3=null;
        System.out.println(array3);*/
        int[] array = null;
        System.out.println(array.length);//NullPointerException空指针异常
        System.out.println(array[0]);//同上
        //null 的作用类似于 C 语言中的 NULL (空指针), 都是表示一个无效的内存位置. 因此不能对这个内存进行任何读写操
        //作. 一旦尝试读写, 就会抛出 NullPointerException


    }

    public static void main2(String[] args) {//数组定义
        String str = "name";
        String str2 = new String("name");
        int[] array = {1, 2, 3, 4, 5};
        int[] array2 = new int[]{1, 2, 3, 4, 5};
        int[] array3 = new int[5];//3个等价
        System.out.println(array[5]);//数组下标越界异常
        System.out.println(array.length);
        int n = 4;
        double[] array4 = new double[n];
        String[] array5 = new String[]{"null", "null", "null", "null"};
        int[] str3;
        str3 = new int[]{1, 2, 3};//不能str3={1,2,3}只有在定义时候才可以一次机会
    }

    public static void main5(String[] args) {
        System.out.println(fib3(40));
        System.out.println(fib2(40));
        System.out.println(fib(40));
        System.out.println(count);
    }

    public static int count = 0;//类的成员函数

    public static int fib3(int n) {
        int[] array = new int[n];
        array[0] = 0;
        array[1] = 1;
        for (int i = 2; i < n; i++) {
            array[i] = array[i - 1] + array[i - 2];
        }
        return array[n - 1];
    }

    public static int fib2(int n) {
        if (n == 1) {
            return 0;
        }
        if (n == 2) {
            return 1;
        }
        if (n == 3) {
            count++;
        }
        return fib2(n - 1) + fib2(n - 2);
    }

    public static int fib(int c) {
        if (c == 1 || c == 2) {
            return 1;
        }
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i < c; i++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }

    public static int sum(int n) {//1729  1+7+2+9
        if (n < 10) {
            return n;
        }
        return n % 10 + sum(n / 10);
    }

    public static void ret2(int n) {
        if (n > 10) {
            ret(n / 10);
        }
        System.out.println(n % 10);
    }

    public static void ret(int n) {//1234  打印1 2 3 4
        if (n < 10) {
            System.out.println(n % 10);
            return;
        }
        ret(n / 10);
        System.out.println(n % 10);

    }

    public static void main1(String[] args) {
        ret2(2345);
      /* int ret=sum(1729);
        System.out.println(ret2(2345));*/
        /*System.out.println(fib2(5));
        System.out.println(fib2(8));*/
       /* System.out.println(fib2(40));
        System.out.println(count);//fib3 执行的次数
        System.out.println(fib(40));*/


    }
}
